# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

_gemname='method_source'
pkgname="ruby-${_gemname}"
pkgver=1.0.0
pkgrel=2
pkgdesc='retrieve the sourcecode for a method'
arch=('any')
url='https://github.com/banister/method_source'
license=('MIT')
options=(!emptydirs)
depends=('ruby')
makedepends=('ruby-bundler' 'ruby-rake' 'ruby-rspec')
source=("${url}/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz")
sha512sums=('1ec606c5de5cf37ca323d645f172eabf02d7adfb1b529e32b97e7c642422906b8501caed4aeb7d66534c3c496eea8918fd2b28d0064487c404658954e920b280')

prepare() {
  cd "${_gemname}-${pkgver}"

  # we built based on a tar archive, not a git repo
  sed --in-place --regexp-extended 's|git ls-files -- spec/*|find spec -type f|' Rakefile
  sed --in-place --regexp-extended 's|git ls-files|find . -type f -not -path "*/.git*"|' Rakefile

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${_gemname}.gemspec"
}

build() {
  cd "${_gemname}-${pkgver}"

  rake ruby:gem
}

check() {
  cd "${_gemname}-${pkgver}"

  rake spec
}

package() {
  cd "${_gemname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  gem install --ignore-dependencies --no-user-install -i "${pkgdir}/${_gemdir}" -n "${pkgdir}/usr/bin" "pkg/${_gemname}-${pkgver}.gem"

  rm "${pkgdir}/${_gemdir}/cache/${_gemname}-${pkgver}.gem"

  install -Dm 644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install -Dm 644 CHANGELOG.md README.markdown --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}
